package com.telecomyt.jwportal;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ProcessLifecycleOwner;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.hailong.appupdate.AppUpdateManager;
import com.ninetripods.sydialoglib.SYDialog;
import com.telecomyt.jwportal.activity.DialogActivity;
import com.telecomyt.jwportal.activity.TextActivity;
import com.telecomyt.jwportal.activity.camera.CameraActivity;
import com.telecomyt.jwportal.activity.channel.FixedChannelActivity;
import com.telecomyt.jwportal.activity.collect.MyCollectActivity;
import com.telecomyt.jwportal.activity.contacts.ContactsListActivity;
import com.telecomyt.jwportal.activity.conversation.ConversationListActivity;
import com.telecomyt.jwportal.activity.drag.DragImageActivity;
import com.telecomyt.jwportal.activity.fragment.FragmentActivity;
import com.telecomyt.jwportal.activity.jsoup.JsoupActivity;
import com.telecomyt.jwportal.activity.login.LoginActivity;
import com.telecomyt.jwportal.activity.message.MessageActivity;
import com.telecomyt.jwportal.activity.person.PersonalDetailsActivity;
import com.telecomyt.jwportal.activity.rich.RichTextActivity;
import com.telecomyt.jwportal.activity.search.SearchActivity;
import com.telecomyt.jwportal.activity.video.VideoActivity;
import com.telecomyt.jwportal.activity.voice.VoiceActivity;
import com.telecomyt.jwportal.activity.work.WorkLogActivity;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.bean.BannerBean;
import com.telecomyt.jwportal.dialog.SearchDialog;
import com.telecomyt.jwportal.http.RetrofitManager;
import com.telecomyt.jwportal.http.base.RxHelper;
import com.telecomyt.jwportal.map.MapActivity;
import com.telecomyt.jwportal.service.GrayService;
import com.telecomyt.jwportal.utils.DateUtils;
import com.telecomyt.jwportal.utils.DistanceUtils;
import com.telecomyt.jwportal.utils.MapUtils;
import com.telecomyt.jwportal.utils.RunTextView;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.telecomyt.jwportal.utils.TwoClickBackExit;
import com.telecomyt.jwportal.webview.WebViewActivity;
import com.telecomyt.jwportal.widget.AvatarUtils;
import com.telecomyt.jwportal.widget.viewpage.bean.DLGridViewBean;
import com.telecomyt.jwportal.widget.viewpage.indicator.CirclePageIndicator;
import com.telecomyt.jwportal.widget.viewpage.utils.DLVPSetting;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.wikikii.bannerlib.banner.IndicatorLocation;
import com.wikikii.bannerlib.banner.LoopLayout;
import com.wikikii.bannerlib.banner.LoopStyle;
import com.wikikii.bannerlib.banner.OnDefaultImageViewLoader;
import com.wikikii.bannerlib.banner.bean.BannerInfo;
import com.wikikii.bannerlib.banner.listener.OnBannerItemClickListener;
import com.wikikii.bannerlib.banner.view.BannerBgContainer;
import com.yanzhenjie.permission.AndPermission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends BaseActivity {
    private static final String[] PHONE_PERMISSIONS = {Manifest.permission.READ_CONTACTS};
    private static final String[] REQUIRED_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final String[] PERMISSIONS = {Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG, Manifest.permission.CALL_PHONE};
    @BindView(R.id.mButton)
    Button mButton;
    @BindView(R.id.mButton1)
    Button mButton1;
    @BindView(R.id.mButton2)
    Button mButton2;
    @BindView(R.id.mButton3)
    Button mButton3;
    @BindView(R.id.mButton4)
    Button mButton4;
    @BindView(R.id.banner_bg_container)
    BannerBgContainer bannerBgContainer;
    @BindView(R.id.loop_layout)
    LoopLayout loopLayout;
    List<Object> bgList = new ArrayList<>();
    // 准备数据
    ArrayList<BannerInfo> bannerInfos = new ArrayList<>();
    @BindView(R.id.mButton5)
    Button mButton5;
    @BindView(R.id.mButton6)
    Button mButton6;
    @BindView(R.id.mButton7)
    Button mButton7;
    @BindView(R.id.mButton8)
    Button mButton8;
    @BindView(R.id.mButton9)
    Button mButton9;
    @BindView(R.id.mButton10)
    Button mButton10;
    @BindView(R.id.mButton12)
    Button mButton12;
    @BindView(R.id.mButton13)
    Button mButton13;
    @BindView(R.id.mButton14)
    Button mButton14;
    @BindView(R.id.mButton15)
    Button mButton15;
    @BindView(R.id.mButton16)
    Button mButton16;
    @BindView(R.id.mButton17)
    Button mButton17;
    @BindView(R.id.mButton18)
    Button mButton18;
    @BindView(R.id.mRunTextView)
    RunTextView mRunTextView;
    @BindView(R.id.mButton11)
    Button mButton11;
    @BindView(R.id.mTvPhone)
    TextView mTvPhone;
    @BindView(R.id.mButton19)
    Button mButton19;
    @BindView(R.id.mButton20)
    Button mButton20;
    @BindView(R.id.mButton21)
    Button mButton21;
    @BindView(R.id.mButton22)
    Button mButton22;
    @BindView(R.id.mButton23)
    Button mButton23;
    List<String> listBeans = new ArrayList<>();
    @BindView(R.id.mViewPager)
    ViewPager viewPager;
    @BindView(R.id.indicator)
    CirclePageIndicator indicator;
    @BindView(R.id.mImageHead)
    ImageView mImageHead;
    private static final String url1 = "http://img1.3lian.com/2015/a2/251/d/296.jpg";
    private static final String url2 = "https://exp.bdstatic.com/static/exp-pc/common/widget/top-search-box/logo_1e63520.png";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;

    }

    @Override
    protected void initView() {
//        mImageHead.setHeadInfo("", "诸葛大力");
        AvatarUtils.setUserAvatar(this, mImageHead, "", url1);
        // 设置轮播图属性
        loopLayout.setLoop_ms(3000);//轮播的速度(毫秒)
        loopLayout.setLoop_duration(400);//滑动的速率(毫秒)
        loopLayout.setScaleAnimation(true);// 设置是否需 要动画
        loopLayout.setLoop_style(LoopStyle.Empty);//轮播的样式-默认empty
        loopLayout.setIndicatorLocation(IndicatorLocation.Right);//指示器位置-中Center
        loopLayout.initializeData(this);
        // 设置轮播图属性end
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                switch (event) {
                    case ON_START:
//                        showFloatingView();
                        break;
                    case ON_STOP:
//                        dismissFloatingView();
                        break;
                }
            }
        });
        mRunTextView.runTextAnimation(1000);
        String standardDate = DateUtils.getStandardDate("2020-9-15 10:44:01");
        String standardDate1 = DateUtils.getStandardDate(1600310641);
        mTvPhone.setText(String.format("%s,%s", standardDate, standardDate1));
        //TODO 验证
//        boolean bankCard = ValidatorUtils.isBankCard("");
//        boolean email = ValidatorUtils.isEmail("");
//        boolean mobile = ValidatorUtils.isMobile("");
//        boolean idCard = ValidatorUtils.isIDCard("");
        double lo1 = 116.403909, la1 = 39.914935;// 天安门
        double lo2 = 116.33942, la2 = 39.904034;// 中盛大厦
        double distance = DistanceUtils.getDistance(lo1, la1, lo2, la2);
        double[] doubles = MapUtils.bd092GCJ(lo1, la1);
        Log.i("MainActivity", "经度" + doubles[0] + "纬度" + doubles[1]);
        initDatas();
    }

    private void startService() {
        Intent grayIntent = new Intent(getApplicationContext(), GrayService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //android8.0以上通过startForegroundService启动service
            startForegroundService(grayIntent);
        } else {
            startService(grayIntent);
        }
    }

    /**
     * @date: 2020/9/28
     * @author: chengDa
     * @description: 填充viewPage数据
     */
    private void initDatas() {
        String[] from = new String[]{"name"};
        int[] to = new int[]{R.id.mTvName};//布局Id
        final ArrayList<Map<String, Object>> dataList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Map<String, Object> bean = new HashMap<>();
            bean.put("name", "测试" + i);
            dataList.add(bean);
        }
        DLVPSetting setting = new DLVPSetting(this, 2, 4, new DLVPSetting.OnClickItemListener() {
            @Override
            public void OnClickItem(int position, DLGridViewBean bean) {
                // 使用默认模式的数据从这里回调
            }

            @Override
            public void OnClickItem(int position, Map<String, Object> map) {
                // 使用自定义模式的数据从这里回调
                ToastUtils.showShort((String) map.get("name"));
            }
        });
        viewPager.setAdapter(setting.getAdapter(dataList, R.layout.activity_conference_items, from, to));
        indicator.setViewPager(viewPager);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    /**
     * 申请权限开启摄像机
     */
    private void requestPermission() {
        AndPermission.with(this)
                .runtime()
                .permission(REQUIRED_PERMISSIONS)
                .onGranted(permissions -> {
                    CameraActivity.start(MainActivity.this);
                })
                .onDenied(permissions -> {
                    Toast.makeText(this, "权限取消", Toast.LENGTH_SHORT).show();
                })
                .start();
    }


    @Override
    protected void initData() {
//        getBannerData();
    }


    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: 设置banner数据
     */
    private void setBannerData() {

        // 设置监听
        loopLayout.setOnLoadImageViewListener(new OnDefaultImageViewLoader() {
            @Override
            public void onLoadImageView(ImageView view, Object object) {
                Glide.with(view.getContext())
                        .load(object)
                        .into(view);
            }
        });
        loopLayout.setOnBannerItemClickListener(new OnBannerItemClickListener() {
            @Override
            public void onBannerClick(int index, ArrayList<BannerInfo> banner) {
                ToastUtils.showShort(index + "");
            }
        });
        if (bannerInfos.size() == 0) {
            return;
        }
        loopLayout.setLoopData(bannerInfos);// 设置轮播数据
        bannerBgContainer.setBannerBackBg(this, bgList);// 背景容器设置轮播图片
        loopLayout.setBannerBgContainer(bannerBgContainer);// 联动
        loopLayout.startLoop();// 开始循环


    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: 获取banner数据
     */
    private void getBannerData() {
        showLoading();
        RetrofitManager.getInstance()
                .getDefaultReq()
                .banner()
                .compose(this.<BannerBean>bindUntilEvent(ActivityEvent.DESTROY))
                .compose(RxHelper.observableIOMain(this))
                .subscribe(new Observer<BannerBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BannerBean registerBean) {
                        dismissLoading();
                        if (registerBean != null) {
                            if (registerBean.getErrorCode() == 0) {
                                if (registerBean.getData() != null) {
                                    for (int i = 0; i < registerBean.getData().size(); i++) {
                                        bannerInfos.add(new BannerInfo(registerBean.getData().get(i).getImagePath(), ""));
                                        bgList.add(registerBean.getData().get(i).getImagePath());
                                    }
                                    setBannerData();
                                }

                            } else {
                                ToastUtils.showShort(registerBean.getErrorMsg());
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();

                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    private String url = "";
    private String[] array = {"优化用户体验", "修复已知bug", "增加在线客服"};

    @Override
    protected BasePresenter creatPresenter() {
        return null;
    }

    @OnClick({R.id.mButton, R.id.mButton1, R.id.mButton2, R.id.mButton3, R.id.mButton4, R.id.mButton5,
            R.id.mButton6, R.id.mButton7, R.id.mButton8, R.id.mButton9, R.id.mButton10, R.id.mButton11,
            R.id.mButton12, R.id.mButton13, R.id.mButton14, R.id.mButton15, R.id.mButton16, R.id.mButton17,
            R.id.mButton18, R.id.mButton19, R.id.mButton20, R.id.mButton21, R.id.mButton22, R.id.mButton23})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mButton23:
                MapActivity.start(this);
//                startService();
                break;
            case R.id.mButton22:
                AndPermission.with(this)
                        .runtime()
                        .permission(PHONE_PERMISSIONS)
                        .onGranted(permissions -> {
                            ContactsListActivity.start(this);
                        })
                        .onDenied(permissions -> {
                            Toast.makeText(this, "权限取消", Toast.LENGTH_SHORT).show();
                        })
                        .start();
//                TextViewActivity.start(this);
                break;
            case R.id.mButton21:
                TextActivity.start(this);
                break;
            case R.id.mButton20:
                DragImageActivity.start(this);
                break;
            case R.id.mButton19:
                FixedChannelActivity.start(this);
                break;
            case R.id.mButton18:
                WorkLogActivity.start(this);
                break;
            case R.id.mButton17:
                VoiceActivity.start(this);
                break;
            case R.id.mButton16:
                AndPermission.with(this)
                        .runtime()
                        .permission(PERMISSIONS)
                        .onGranted(permissions -> {
                            ConversationListActivity.start(this);
                        })
                        .onDenied(permissions -> {
                            Toast.makeText(this, "权限取消", Toast.LENGTH_SHORT).show();
                        })
                        .start();
                break;
            case R.id.mButton15://TODO 我的收藏
                MyCollectActivity.start(this);
                break;
            case R.id.mButton14:
                MessageActivity.start(this);
                break;
            case R.id.mButton13:
                JsoupActivity.start(this);
                break;
            case R.id.mButton12:
                WebViewActivity.start(this);
                break;
            case R.id.mButton11:
                RichTextActivity.start(this);
//                FaceActivity.start(this);
                break;
            case R.id.mButton:
                requestPermission();
//                BehaviorActivity.start(this);
                break;
            case R.id.mButton1:
                FragmentActivity.start(this);
                break;
            case R.id.mButton2:
                LoginActivity.start(this);
                break;
            case R.id.mButton3:
                AppUpdateManager.Builder builder = new AppUpdateManager.Builder(MainActivity.this);
                builder.apkUrl(url)
                        .updateContent(array)
                        .updateForce(false)
                        .newVerName("1.2.2")
                        .title("发现新版本")
                        .confirmText("立即更新")
                        .cancelText("取消更新")
                        .build();

//                RegisterActivity.start(this);
                break;
            case R.id.mButton4://文本dialog
//                new DialogUtils(this, 1, "是否确认撤回上报内容?", "否", "是", new DialogUtils.ConfirmListener() {
//                    @Override
//                    public void onConfirm() {
//                        ToastUtils.showShort("是");
//
//                    }
//                }).show();
                DialogActivity.start(this);
//                new SYDialog.Builder(this)
//                        .setDialogView(R.layout.layout_dialog_text)//设置dialog布局
//                        .setAnimStyle(R.style.DialogStyle)//设置动画 默认没有动画
//                        .setScreenWidthP(0.85f) //设置屏幕宽度比例 0.0f-1.0f
//                        .setGravity(Gravity.CENTER)//设置Gravity
//                        .setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
//                        .setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽  false屏蔽
//                        .setCancelableOutSide(true)//设置dialog外点击是否可以让dialog消失
//                        .setBuildChildListener((dialog, view1, layoutRes) -> {
//                            TextView mTvMessage = view1.findViewById(R.id.mTvMessage);
//                            mTvMessage.setText("标题");
//                            view1.findViewById(R.id.mTvCancel).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View view) {
//                                    dialog.dismiss();
//                                }
//                            });
//                            view1.findViewById(R.id.mTvConfirm).setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View view) {
//                                    ToastUtils.showShort("是");
//                                    dialog.dismiss();
//                                }
//                            });
//                        }).show();
                break;
            case R.id.mButton5://分享dialog
//                new DialogUtils(this, 2, "张三", "", "", "取消", "发送", new DialogUtils.ConfirmListener() {
//                    @Override
//                    public void onConfirm() {
//                        ToastUtils.showShort("发送");
//                    }
//                }).show();
                new SYDialog.Builder(this)
                        .setDialogView(R.layout.layout_dialog_share)//设置dialog布局
                        .setAnimStyle(R.style.DialogStyle)//设置动画 默认没有动画
                        .setScreenWidthP(0.85f) //设置屏幕宽度比例 0.0f-1.0f
                        .setGravity(Gravity.CENTER)//设置Gravity
                        .setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
                        .setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽  false屏蔽
                        .setCancelableOutSide(true)//设置dialog外点击是否可以让dialog消失
                        .setBuildChildListener((dialog, view1, layoutRes) -> {
                            ImageView mIvShareHead = (ImageView) findViewById(R.id.mIvShareHead);
                            ImageView mIvShareContent = (ImageView) findViewById(R.id.mIvShareContent);
                            TextView mTvShareName = (TextView) findViewById(R.id.mTvShareName);
                            view1.findViewById(R.id.mTvCancel).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    dialog.dismiss();
                                }
                            });
                            view1.findViewById(R.id.mTvConfirm).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    ToastUtils.showShort("分享");
                                    dialog.dismiss();
                                }
                            });

                        }).show();
                break;
            case R.id.mButton6://输入框dialog
//                new DialogUtils(this, 3, "取消", "确定", new DialogUtils.ModificationListener() {
//                    @Override
//                    public void onConfirmName(String name) {
//                        ToastUtils.showShort(name);
//
//                    }
//                }).show();
                new SYDialog.Builder(this)
                        .setDialogView(R.layout.layout_dialog_edit)//设置dialog布局
                        .setAnimStyle(R.style.DialogStyle)//设置动画 默认没有动画
                        .setScreenWidthP(0.85f) //设置屏幕宽度比例 0.0f-1.0f
                        .setGravity(Gravity.CENTER)//设置Gravity
                        .setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
                        .setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽  false屏蔽
                        .setCancelableOutSide(true)//设置dialog外点击是否可以让dialog消失
                        .setBuildChildListener((dialog, view1, layoutRes) -> {
                            EditText mEditText = view1.findViewById(R.id.mEditText);
                            view1.findViewById(R.id.mTvCancel).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    dialog.dismiss();
                                }
                            });
                            view1.findViewById(R.id.mTvConfirm).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    ToastUtils.showShort(mEditText.getText().toString().trim());
                                    dialog.dismiss();
                                }
                            });

                        }).show();
                break;
            case R.id.mButton7://一张大图dialog
//                new DialogUtils(this, 4, "");
                new SYDialog.Builder(this)
                        .setDialogView(R.layout.layout_dialog_image)//设置dialog布局
                        .setAnimStyle(R.style.DialogStyle)//设置动画 默认没有动画
                        .setScreenWidthP(0.85f) //设置屏幕宽度比例 0.0f-1.0f
                        .setGravity(Gravity.CENTER)//设置Gravity
                        .setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
                        .setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽  false屏蔽
                        .setCancelableOutSide(true)//设置dialog外点击是否可以让dialog消失
                        .setBuildChildListener((dialog, view1, layoutRes) -> {
                            ImageView mImageView = view1.findViewById(R.id.mImageView);
//                            GlideUtils.load(getContext(), imagePath, mImageView);//加载头像
                            view1.findViewById(R.id.mLlCancle).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    dialog.dismiss();
                                }
                            });

                        }).show();
                break;
            case R.id.mButton8://呼叫dialog
                new SYDialog.Builder(this)
                        .setDialogView(R.layout.layout_dialog_call)//设置dialog布局
                        .setAnimStyle(R.style.DialogStyle)//设置动画 默认没有动画
                        .setScreenWidthP(0.85f) //设置屏幕宽度比例 0.0f-1.0f
                        .setGravity(Gravity.CENTER)//设置Gravity
                        .setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
                        .setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽  false屏蔽
                        .setCancelableOutSide(true)//设置dialog外点击是否可以让dialog消失
                        .setBuildChildListener((dialog, view1, layoutRes) -> {
                            TextView mTvTitle = view1.findViewById(R.id.mTvTitle);
                            mTvTitle.setText("正在呼叫某某某,请稍后...");
                            TextView mTvHangUp = view1.findViewById(R.id.mTvHangUp);
                            mTvHangUp.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view1) {
                                    dialog.dismiss();
                                }
                            });
                        }).show();
//                searchDialog = new SearchDialog(this, new SearchDialog.ClickListener() {
//                    @Override
//                    public void onRise(LinearLayout mLinearLayout) {
//                        setDialogHeight(true, searchDialog);
//
//                    }
//
//                    @Override
//                    public void down(LinearLayout mLinearLayout) {
//                        setDialogHeight(false, searchDialog);
//                    }
//                });
//                searchDialog.show();

                break;
            case R.id.mButton9:
                VideoActivity.start(this);
                break;
            case R.id.mButton10://TODO 个人信息
                PersonalDetailsActivity.start(this, 1453, 110);
                break;

        }
    }

    /**
     * 设置 dialog的高度
     *
     * @param dialog
     */
    private void setDialogHeight(boolean isFlag, SearchDialog dialog) {
        WindowManager m = getWindowManager();
        Display display = m.getDefaultDisplay();
        WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
        if (isFlag) {
            p.height = (int) (display.getHeight() * 0.9);
        } else {
            p.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        }
        dialog.getWindow().setAttributes(p);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (TwoClickBackExit.getInstance().isExit()) {
                finish();
            } else {
                TwoClickBackExit.getInstance().doExitInOneSecond();
                ToastUtils.showShort("再按一次退出程序");
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}